package com.app.qbake.module.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.TextView;

import com.wind.base.mvp.view.BaseLoadingFragment;
import com.wind.base.request.BaseRequest;
import com.wind.base.utils.ActivityUtil;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.mine.GetAlipayAccountRequest;
import com.wind.data.mine.GetAlipayAccountResponse;
import com.wind.data.mine.WithdrawMoneyRequest;
import com.wind.data.mine.WithdrawMoneyResponse;
import com.wind.data.mine.bean.AlipayAccount;
import com.app.qbake.R;
import com.app.qbake.module.mine.activity.EditAlipayAccountActivity;
import com.app.qbake.module.mine.di.GetAlipayAccountComponent;
import com.app.qbake.module.mine.event.EditAlipayAccountSuccessEvent;
import com.app.qbake.module.mine.layout.WithdrawMoneyLayout;
import com.app.qbake.module.mine.mvp.GetAlipayAccountView;
import com.app.qbake.module.mine.presenter.GetAlipayAccountPresenter;
import com.app.qbake.util.FormatUtil;
import com.wind.toastlib.ToastUtil;
import com.wind.view.ValidateEditText;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wind on 2018/10/20.
 */
@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.mine.mvp.GetAlipayAccountView",
                responseCanonicalName = "com.wind.data.mine.GetAlipayAccountResponse",
                requestCanonicalName = "com.wind.data.mine.GetAlipayAccountRequest"

        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "user/getUserCash")
)
public class GetAlipayAccountFragment extends BaseLoadingFragment<GetAlipayAccountView, GetAlipayAccountPresenter, GetAlipayAccountComponent, GetAlipayAccountResponse>
        implements GetAlipayAccountView {

    public static final String ARGS_KEY_AMOUNT="args_key_amount";
    @BindView(R.id.tv_add_alipay)
    TextView tv_add_alipay;

    @BindView(R.id.layout_account)
    View layout_account;
    @BindView(R.id.tv_alipay_account)
    TextView tv_alipay_account;

    @BindView(R.id.tv_withdraw_money)
    ValidateEditText tv_withdraw_money;

    @BindView(R.id.tv_total)
    TextView tv_total;//可提现余额
    @Inject
    GetAlipayAccountPresenter presenter;
    WithdrawMoneyLayout mWithdrawMoneyLayout;

    private double amount;//可提现金额
    @Override
    public GetAlipayAccountPresenter createPresenter() {
        return presenter;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_withdraw_money;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        amount=getArguments().getDouble(ARGS_KEY_AMOUNT,0);
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        mWithdrawMoneyLayout=new WithdrawMoneyLayout(getActivity(), new WithdrawMoneyLayout.OnWithdrawListener() {
            @Override
            public void onWithdrawSuccess(WithdrawMoneyResponse response) {
                ActivityUtil.finish(getActivity());
            }
        });

        tv_total.setText("可提现余额：￥"+amount);
    }

    @Override
    protected BaseRequest buildLoadingRequest() {
        GetAlipayAccountRequest request = new GetAlipayAccountRequest();
        return request;
    }


    @Override
    public void showPage(GetAlipayAccountResponse reponse) {

        List<AlipayAccount> accounts = reponse.getData();
        if (accounts.isEmpty()) {
            tv_add_alipay.setVisibility(View.VISIBLE);
            layout_account.setVisibility(View.GONE);

        } else {
            tv_add_alipay.setVisibility(View.GONE);
            layout_account.setVisibility(View.VISIBLE);
            mAlipayAccount=accounts.get(0);
            tv_alipay_account.setText(mAlipayAccount.getAccount_number()+"("+mAlipayAccount.getAccount_name()+")");
        }
        mLayoutManager.showContent();
    }


    @OnClick({R.id.tv_add_alipay,R.id.tv_edit_account,R.id.tv_confirm})
    public void onViewClick(View view){
        switch (view.getId()){
            case R.id.tv_confirm:
                if (validate()){
                    String withdrawMoney=getText(tv_withdraw_money);
                    WithdrawMoneyRequest request=new WithdrawMoneyRequest();
                    request.setAccount_name(mAlipayAccount.getAccount_name());
                    request.setAccount_number(mAlipayAccount.getAccount_number());
                    request.setAmount(withdrawMoney);
                    mWithdrawMoneyLayout.withdraw(request);
                }

                break;
            case R.id.tv_edit_account:
                EditAlipayAccountActivity.start(getActivity(),mAlipayAccount);
                break;
            case R.id.tv_add_alipay:
                EditAlipayAccountActivity.start(getActivity());
                break;
        }
    }

    private boolean validate() {
        if (mAlipayAccount==null){
            ToastUtil.showToast(getActivity(),"请输入支付宝账号");
            return false;
        }

        boolean pass=tv_withdraw_money.validate("请输入提现金额");
        if (pass){
            double inputMoney=Double.parseDouble(getText(tv_withdraw_money));
            if (inputMoney>amount){
                String amountS=FormatUtil.formatMoney(amount);
                ToastUtil.showToast(getActivity(),"最多可提现"+amountS+"元");
                return false;
            }

        }
        return true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    private AlipayAccount mAlipayAccount;
    @Subscribe
    public void onEditAlipayAccountSuccessEvent(EditAlipayAccountSuccessEvent event){
        mAlipayAccount=event.getAccount();
        tv_add_alipay.setVisibility(View.GONE);
        layout_account.setVisibility(View.VISIBLE);
        tv_alipay_account.setText(mAlipayAccount.getAccount_number()+"("+mAlipayAccount.getAccount_name()+")");
    }

    public static Fragment getInstance(double amount) {
        Fragment f=new GetAlipayAccountFragment();
        Bundle args=new Bundle();
        args.putDouble(ARGS_KEY_AMOUNT, amount);
        f.setArguments(args);
        return f;
    }
}
